/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainForm.java
 *
 * Created on 09/09/2011, 23:44:26
 */
package br.edu.unijorge.form;

import br.edu.unijorge.entidade.Exercito;
import br.edu.unijorge.entidade.Peca;
import br.edu.unijorge.entidade.PecaMovel;
import br.edu.unijorge.entidade.Posicao;
import br.edu.unijorge.entidade.Tabuleiro;
import br.edu.unijorge.util.UtilX;
import java.awt.Component;
import javax.swing.JOptionPane;

/**
 *
 * @author Glauber
 */
public class MainForm extends javax.swing.JFrame {

    public static final int LAYER_TABULEIRO = 0;
    public static final int LAYER_MSG = 400;

    public MainForm() {
        initComponents();
        //setBounds(0, 0, Tabuleiro.LARGURA + 10, Tabuleiro.ALTURA + 55);
        tabuleiro = Tabuleiro.getInstance();
        UtilX.centerX(this);
        setResizable(false);
    }

    private void inicializarTabuleiro() {
        jpTabuleiro.remove(tabuleiro);
        jpTabuleiro.add(tabuleiro);
        tabuleiro.iniciarJogo();
        jpSlotAzul.add(tabuleiro.getSlotAzul());
        jpSlotVerm.add(tabuleiro.getSlotVerm());
        jpSlotAzul.updateUI();
        jpSlotVerm.updateUI();
        repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpMain = new javax.swing.JPanel();
        jpSlotAzul = new javax.swing.JPanel();
        jpTabuleiro = new javax.swing.JPanel();
        jpSlotVerm = new javax.swing.JPanel();
        jmbMenu = new javax.swing.JMenuBar();
        jmJogo = new javax.swing.JMenu();
        jmNovoJogo = new javax.swing.JMenu();
        jmiNovoJogoLocal = new javax.swing.JMenuItem();
        jmiNovoJogoRede = new javax.swing.JMenuItem();
        jsJogo1 = new javax.swing.JPopupMenu.Separator();
        jmiSair = new javax.swing.JMenuItem();
        jmAjuda = new javax.swing.JMenu();
        jmiRegras = new javax.swing.JMenuItem();
        jmiSobre = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });

        jpSlotAzul.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jpSlotAzul.setPreferredSize(new java.awt.Dimension(260, 650));

        javax.swing.GroupLayout jpSlotAzulLayout = new javax.swing.GroupLayout(jpSlotAzul);
        jpSlotAzul.setLayout(jpSlotAzulLayout);
        jpSlotAzulLayout.setHorizontalGroup(
            jpSlotAzulLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 258, Short.MAX_VALUE)
        );
        jpSlotAzulLayout.setVerticalGroup(
            jpSlotAzulLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 648, Short.MAX_VALUE)
        );

        jpTabuleiro.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jpTabuleiro.setMaximumSize(new java.awt.Dimension(650, 650));
        jpTabuleiro.setMinimumSize(new java.awt.Dimension(650, 650));
        jpTabuleiro.setPreferredSize(new java.awt.Dimension(650, 650));

        javax.swing.GroupLayout jpTabuleiroLayout = new javax.swing.GroupLayout(jpTabuleiro);
        jpTabuleiro.setLayout(jpTabuleiroLayout);
        jpTabuleiroLayout.setHorizontalGroup(
            jpTabuleiroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 648, Short.MAX_VALUE)
        );
        jpTabuleiroLayout.setVerticalGroup(
            jpTabuleiroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 648, Short.MAX_VALUE)
        );

        jpSlotVerm.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jpSlotVerm.setPreferredSize(new java.awt.Dimension(260, 650));

        javax.swing.GroupLayout jpSlotVermLayout = new javax.swing.GroupLayout(jpSlotVerm);
        jpSlotVerm.setLayout(jpSlotVermLayout);
        jpSlotVermLayout.setHorizontalGroup(
            jpSlotVermLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 258, Short.MAX_VALUE)
        );
        jpSlotVermLayout.setVerticalGroup(
            jpSlotVermLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 648, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jpMainLayout = new javax.swing.GroupLayout(jpMain);
        jpMain.setLayout(jpMainLayout);
        jpMainLayout.setHorizontalGroup(
            jpMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpMainLayout.createSequentialGroup()
                .addComponent(jpSlotAzul, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jpTabuleiro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jpSlotVerm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jpMainLayout.setVerticalGroup(
            jpMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpSlotAzul, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jpSlotVerm, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jpTabuleiro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jmJogo.setText("Jogo");

        jmNovoJogo.setText("Novo Jogo");

        jmiNovoJogoLocal.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
        jmiNovoJogoLocal.setText("Local");
        jmiNovoJogoLocal.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jmiNovoJogoLocalMouseReleased(evt);
            }
        });
        jmNovoJogo.add(jmiNovoJogoLocal);

        jmiNovoJogoRede.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3, 0));
        jmiNovoJogoRede.setText("Rede...");
        jmiNovoJogoRede.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jmiNovoJogoRedeMouseReleased(evt);
            }
        });
        jmNovoJogo.add(jmiNovoJogoRede);

        jmJogo.add(jmNovoJogo);
        jmJogo.add(jsJogo1);

        jmiSair.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        jmiSair.setText("Sair");
        jmiSair.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jmiSairMouseReleased(evt);
            }
        });
        jmJogo.add(jmiSair);

        jmbMenu.add(jmJogo);

        jmAjuda.setText("Ajuda");

        jmiRegras.setText("Regras...");
        jmiRegras.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jmiRegrasMouseReleased(evt);
            }
        });
        jmAjuda.add(jmiRegras);

        jmiSobre.setText("Sobre...");
        jmiSobre.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jmiSobreMouseReleased(evt);
            }
        });
        jmAjuda.add(jmiSobre);

        jmbMenu.add(jmAjuda);

        setJMenuBar(jmbMenu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jmiSairMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jmiSairMouseReleased
        if (JOptionPane.showConfirmDialog(
                this,
                "Tem certeza que deseja encerrar o jogo?",
                "Encerrar Jogo",
                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            this.dispose();
        }
    }//GEN-LAST:event_jmiSairMouseReleased

    private void jmiNovoJogoLocalMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jmiNovoJogoLocalMouseReleased
        inicializarTabuleiro();
}//GEN-LAST:event_jmiNovoJogoLocalMouseReleased

    private void jmiRegrasMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jmiRegrasMouseReleased
        if(rf == null){
            rf = new RegrasForm(this, false);
            rf.setVisible(true);
        }else{
            rf.setVisible(true);
        }
    }//GEN-LAST:event_jmiRegrasMouseReleased

    private void jmiSobreMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jmiSobreMouseReleased
        if (sf == null) {
            sf = new SobreForm(this, true);
            sf.setVisible(true);
        }else{
            sf.setVisible(true);
        }
    }//GEN-LAST:event_jmiSobreMouseReleased

    private void jmiNovoJogoRedeMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jmiNovoJogoRedeMouseReleased
        if(jr == null){
            jr = new NovoJogoRedeForm(this, true);
            jr.setVisible(true);
        }else{
            jr.setVisible(true);
        }
    }//GEN-LAST:event_jmiNovoJogoRedeMouseReleased

    private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
        Component c = evt.getComponent();
        System.out.print(c);
    }//GEN-LAST:event_formMouseReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainForm().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jmAjuda;
    private javax.swing.JMenu jmJogo;
    private javax.swing.JMenu jmNovoJogo;
    private javax.swing.JMenuBar jmbMenu;
    private javax.swing.JMenuItem jmiNovoJogoLocal;
    private javax.swing.JMenuItem jmiNovoJogoRede;
    private javax.swing.JMenuItem jmiRegras;
    private javax.swing.JMenuItem jmiSair;
    private javax.swing.JMenuItem jmiSobre;
    private javax.swing.JPanel jpMain;
    private javax.swing.JPanel jpSlotAzul;
    private javax.swing.JPanel jpSlotVerm;
    private javax.swing.JPanel jpTabuleiro;
    private javax.swing.JPopupMenu.Separator jsJogo1;
    // End of variables declaration//GEN-END:variables
    Tabuleiro tabuleiro;
    RegrasForm rf;
    SobreForm sf;
    NovoJogoRedeForm jr;
}
